package com.sykj.bsc.zjj.controller;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.zjj.bean.Bis_shop;
import com.sykj.bsc.zjj.bean.Bis_shop_post;
import com.sykj.bsc.zjj.bean.Bis_shop_user;
import com.sykj.bsc.zjj.service.Bis_shop_postService;
import com.sykj.bsc.zjj.service.Bis_shop_userService;

@Controller
@RequestMapping("zjj")
public class Bis_shop_userController {
	@Autowired
	private Bis_shop_userService bsus;
	@Autowired
	private Bis_shop_postService bsps;
	
	/**
	 * 查询
	 * 店铺用户信息表
	 * 店铺信息表
	 * 店铺职位信息表
	 * @param map
	 * @param bsu
	 * @return
	 */
	@RequestMapping(value="/selectBis_shop_user")
	public String selectBis_shop_user(Map<String, Object> map,Model model,Bis_shop_user bis_shop_user,@RequestParam(value = "page", defaultValue = "1") Integer page) {
		PageHelper.startPage(page,5);
		//定值
		if(bis_shop_user!=null) getBis_shop_user1(model, bis_shop_user);
		
		/**
		 * 查询
		 * 店铺用户信息表
		 * 店铺信息表
		 * 店铺职位信息表
		 */
		List<Map> list = this.bsus.selectBis_shop_user(bis_shop_user);
		/*map.put("list", list);*/
		PageInfo<Map> info = new PageInfo<>(list);
		map.put("info", info);
		
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bsus.selectBis_shop();
		map.put("list2", list2);
		/**
		 * 店铺职位信息表
		 */
		List<Bis_shop_post> list3 = this.bsus.seListBis_shop_post();
		map.put("list3", list3);
		return "backstage/zjj/staff";
	}
	
	/**
	 * 条件查询定值方法
	 * @param model
	 * @param bsu
	 */
	private void getBis_shop_user1(Model model,Bis_shop_user bsu) {
		if (bsu.getAccount()!=null && !bsu.getAccount().equals("")) {
			model.addAttribute("account", bsu.getAccount());
		}
		if (bsu.getUsername()!=null && !bsu.getUsername().equals("")) {
			model.addAttribute("username", bsu.getUsername());
		}
		if (bsu.getWork_number()!=null && !bsu.getWork_number().equals("")) {
			model.addAttribute("work_number", bsu.getWork_number());
		}
		if (bsu.getShop_id()!=null && !bsu.getShop_id().equals("")) {
			model.addAttribute("shop_id", bsu.getShop_id());
		}
		if (bsu.getPost_code()!=null && !bsu.getPost_code().equals("")) {
			model.addAttribute("post_code", bsu.getPost_code());
		}
		if (bsu.getMobile()!=null && !bsu.getMobile().equals("")) {
			model.addAttribute("mobile", bsu.getMobile());
		}
		if (bsu.getIdno()!=null && !bsu.getIdno().equals("")) {
			model.addAttribute("idno", bsu.getIdno());
		}
		if (bsu.getStatus()!=null &&!bsu.getStatus().equals("")) {
			model.addAttribute("status", bsu.getStatus());
		}
	}
	
	/**
	 * 跳转到添加界面
	 * 店铺用户信息表
	 * @param bis_shop_user
	 * @return
	 */
	@RequestMapping(value="/staffinsert")
	public String staffinsert(Map<String, Object> map) {
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bsus.selectBis_shop();
		map.put("list2", list2);
		/**
		 * 店铺职位信息表
		 */
		List<Bis_shop_post> list3 = this.bsus.seListBis_shop_post();
		map.put("list3", list3);
		return "backstage/zjj/staffinsert";
	}
	
	/**
	 * 添加
	 * 店铺用户信息表
	 * @param bis_shop_user
	 * @return
	 */
	@RequestMapping(value="/insertBis_shop_user",method = RequestMethod.POST)
	public String insertBis_shop_user(Bis_shop_user bis_shop_user,@RequestParam("pic") MultipartFile file ,HttpServletRequest request,HttpSession session) {
		/**
		 * 生成随机用户编号
		 */
		SimpleDateFormat sdf1=new SimpleDateFormat("yyMMddHHmmss");
		String str1=sdf1.format(new Date());
		Random random1=new Random();
		int num1= random1.nextInt(9999)+1000;
		String shop_user_id=str1+num1;
		
		//上传
		String path=request.getRealPath("/");
		File file1=new File(path);
		/*path=file1.getParent();*/
		path=path+"/upload/";
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		path=path+sdf.format(date);
		File dir =new File(path);
		if (!dir.exists()) {
			//如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File newfile=new File(path, file.getOriginalFilename());
		try {
			if (!newfile.exists()) {
				//此时创建之后只是一个空的文件
				newfile.createNewFile();
		}
			//将file中的内容复制到newfile中
			file.transferTo(newfile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		bis_shop_user.setPhoto("/upload/"+sdf.format(date)+"/"+file.getOriginalFilename());
		
		//密码加密
		String hashAlgorithName="MD5";
		String pass=bis_shop_user.getPassword();
		//盐值
		Object salt=ByteSource.Util.bytes(bis_shop_user.getAccount());
		//加密次数
		int count=1024;
		//进行加密
		Object result=new SimpleHash(hashAlgorithName, pass,salt,count);
		bis_shop_user.setPassword(result.toString());
		
		//session
		//bis_shop_user.setCreate_user_id("cb33c25f5c664058a111a9b876152317");//创建人ID
		// 创建人ID
		String userId = this.getUser(session);
		bis_shop_user.setCreate_user_id(userId);
		
		bis_shop_user.setShop_user_id(shop_user_id);
		this.bsus.insertBis_shop_user(bis_shop_user);
		return "redirect:/zjj/selectBis_shop_user";
	}
	
	// session
	private String getUser(HttpSession session) {
		SysUser sysUser = (SysUser) session.getAttribute("user");
		String userId = sysUser.getUser_id() + "";
		return userId;
	}
	
	/**
	 * 根据ID查询
	 * Bis_shop_user
	 * 店铺用户信息表
	 * 跳转到修改界面
	 * @param shop_user_id
	 * @param map
	 * @return
	 */
	@RequestMapping("findByIdBis_shop_user")
	public String findByIdBis_shop_user(String shop_user_id,Map<String, Object> map) {
		/**
		 * 根据ID查询
		 * Bis_shop_user
		 * 店铺用户信息表
		 */
		Bis_shop_user bis_shop_user = this.bsus.findByIdBis_shop_user(shop_user_id);
		map.put("bis_shop_user", bis_shop_user);
		/**
		 * 查询
		 * 店铺信息表
		 */
		List<Bis_shop> list2 = this.bsus.selectBis_shop();
		map.put("list2", list2);
		/**
		 * 店铺职位信息表
		 */
		List<Bis_shop_post> list3 = this.bsus.seListBis_shop_post();
		map.put("list3", list3);		
		return "backstage/zjj/staffupdate";
	}
	
	/**
	 * 修改
	 * Bis_shop_user
	 * 店铺用户信息表
	 * @param bis_shop_user
	 * @return
	 */
	@RequestMapping(value="/updateBis_shop_user",method = RequestMethod.POST)
	public String updateBis_shop_user(Bis_shop_user bis_shop_user,@RequestParam("pic") MultipartFile file ,HttpServletRequest request,HttpSession session) {
		// 上传
		String path = request.getRealPath("/");// tomcat/project
		File file1 = new File(path);
		// path=file1.getParent();//webapp
		path = path + "/upload/";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		// path:c:/tools/tomcat/webapp/upload/20180719
		path = path + sdf.format(date);
		File dir = new File(path);
		if (!dir.exists()) {
			// 如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File photo = new File(path, file.getOriginalFilename());
		try {
			if (!photo.exists()) {
				// 此时创建之后只是一个空的文件
				photo.createNewFile();
			}
			// 将file中的内容复制到newfile中
			file.transferTo(photo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String a = file.getOriginalFilename();
		if (a != null && !a.equals("")) {
			bis_shop_user.setPhoto("/upload/" + sdf.format(date) + "/" + file.getOriginalFilename());
		}
		
		//session
		//bis_shop_user.setModify_user_id("cb33c25f5c664058a111a9b876152317");//修改用户ID
		// 修改用户ID
		String userId = this.getUser(session);
		bis_shop_user.setModify_user_id(userId);
		
		this.bsus.updateBis_shop_user(bis_shop_user);
		return "redirect:/zjj/selectBis_shop_user";
	}
	
	/**
	 * 删除
	 * 这里是修改了用户状态
	* Bis_shop_user
	 * 店铺用户信息表
	 * @param bis_shop_user
	 * @return
	 */
	@RequestMapping("deleteBis_shop_user")
	public String deleteBis_shop_user(Bis_shop_user bis_shop_user) {
		this.bsus.deleteBis_shop_user(bis_shop_user);
		return "redirect:/zjj/selectBis_shop_user";
	}
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工账户是否重复
	 * @param account
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_useraccount")
	public void findByIdBis_shop_useraccount(Bis_shop_user account,HttpServletRequest request,HttpServletResponse response) {
		String account1=request.getParameter("account");
		Integer account11 = this.bsus.findByIdBis_shop_useraccount(account1);
		boolean b1;
		if (account11==0) {
			b1 = true;
		} else {
			b1 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b1);
		resjson(response, map);
	}
	private void resjson(HttpServletResponse response,Object obj){
    	ObjectMapper  om=new ObjectMapper();
    	try {
			String json=om.writeValueAsString(obj);
			PrintWriter out=response.getWriter();
			out.write(json);
			out.flush();
			out.close();
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工手机号是否重复
	 * @param mobile
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_usermobile")
	public void findByIdBis_shop_usermobile(Bis_shop_user mobile,HttpServletRequest request,HttpServletResponse response) {
		String mobile1=request.getParameter("mobile");
		Integer mobile11 = this.bsus.findByIdBis_shop_usermobile(mobile1);
		boolean b2;
		if (mobile11==0) {
			b2 = true;
		} else {
			b2 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b2);
		resjson(response, map);
	}
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工身份证是否重复
	 * @param idno
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_useridno")
	public void findByIdBis_shop_useridno(Bis_shop_user idno,HttpServletRequest request,HttpServletResponse response) {
		String idno1=request.getParameter("idno");
		Integer idno11 = this.bsus.findByIdBis_shop_useridno(idno1);
		boolean b3;
		if (idno11==0) {
			b3 = true;
		} else {
			b3 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b3);
		resjson(response, map);
	}
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工邮箱是否重复
	 * @param email
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_useremail")
	public void findByIdBis_shop_useremail(Bis_shop_user email,HttpServletRequest request,HttpServletResponse response) {
		String email1=request.getParameter("email");
		Integer email11 = this.bsus.findByIdBis_shop_useremail(email1);
		boolean b4;
		if (email11==0) {
			b4 = true;
		} else {
			b4 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b4);
		resjson(response, map);
	}
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工电话是否重复
	 * @param phone
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_userphone")
	public void findByIdBis_shop_userphone(Bis_shop_user phone,HttpServletRequest request,HttpServletResponse response) {
		String phone1=request.getParameter("phone");
		Integer phone11 = this.bsus.findByIdBis_shop_userphone(phone1);
		boolean b5;
		if (phone11==0) {
			b5 = true;
		} else {
			b5 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b5);
		resjson(response, map);
	}
	
	/**
	 * 查询店铺员工信息表
	 * 判断员工紧急联系电话是否重复
	 * @param linkphone
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_userlinkphone")
	public void findByIdBis_shop_userlinkphone(Bis_shop_user linkphone,HttpServletRequest request,HttpServletResponse response) {
		String linkphone1=request.getParameter("linkphone");
		Integer linkphone11 = this.bsus.findByIdBis_shop_userlinkphone(linkphone1);
		boolean b6;
		if (linkphone11==0) {
			b6 = true;
		} else {
			b6 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b6);
		resjson(response, map);
	}
	
	@RequestMapping(value="selectMenu",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String selectMenu(HttpServletResponse response) {
		String json=bsus.selectShopMenu();
		return json;
	}
	@RequestMapping(value="selectMenu2",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String selectMenu2(HttpServletResponse response,String post_id) {
		String json=bsps.checkPost_menu(post_id);
		return json;
	}
	
	/*@RequestMapping("selectMenu")
	public String selectMenu(Model model) {
		System.out.println("555555555555");
		String json=bsus.selectShopMenu();
		model.addAttribute("data",json);
		return "zjj/positionselect";
	}*/
}
